<?php

class messages extends Controller
{
	
	public function index () {
		$this->session();
		$data['title'] = 'Messages - iTamCity';
		$model = new pm();
		$messages = $model->getInbox($_SESSION['stud_no']);
		$inbox = $model->countInbox($_SESSION['stud_no']);
		$data['content'] = 
			BufferedView::instance('messages/inbox',array(
				"messages" => $messages,
			) )->getBuffer();
		$data['heading'] = 'Inbox';
		$data['heading'] = 'Inbox';
		$data['globalnav'] = new BufferedView('org/globalnav');
		$data['usernav'] = BufferedView::instance('messages/nav', array('highlight'=>'inbox') );
		new View ("template", $data);
	}

   public function show($messageId, $senderId = NULL) {
		$this->session();
		//$data['title'] = new BufferedView('members/tviewMessage');
		$model = new pm();
		$model->read( $messageId );
		$_SESSION['inboxUnread'] = $model->countUnread( $_SESSION['stud_no'] );
		$inbox = $model->countInbox($_SESSION['stud_no']);
		if( $senderId === NULL ) $messagebyId = $model->getMessagebyID($_SESSION['stud_no'],$messageId); 
		else $messagebyId = $model->getsentbyID($senderId,$messageId); 
		$data['title'] = 'Messages- iTamCity';
		$data['content'] = BufferedView::instance('messages/view',array(
			 'message' => $messagebyId[0],
			 'referrer' => ( $senderId === NULL ? 'inbox' : 'sent' )
			) )->getBuffer();	
      $data['globalnav'] = new BufferedView('org/globalnav');  
		$data['usernav'] = BufferedView::instance('messages/nav');
		new View ("template", $data);
	}
	
	public function find_user() {
		$this->session();
		$data['title'] = 'Messages - iTamCity';
		$model = new user();
		if( isset($_POST['search']{1}) ) {
			$results = $model->findUserByName( $_POST['search'] ) ;
			$data['content'] = 
			BufferedView::instance('members/forms/search',array(
				"results" => (array) $results
			))->getBuffer();
			if( is_array( $results ) && count( $results ) === 0 ) $data['content'] .= 'No hits found';
		} else {
			$data['content'] = 'Please enter at least 2 characters for the search' 
				. BufferedView::instance('members/forms/search',array(
					"results" => (array) $results
				))->getBuffer();
		}
		
		$data['heading'] = 'Find recipient';
		$data['globalnav'] = new BufferedView('org/globalnav');
		$data['usernav'] = new BufferedView('messages/nav', array('highlight'=>'create'));
		//$data['usernav'] = BufferedView::instance('messages/nav', array('highlight'=>'create') );		
		new View ("template", $data);
	}

	public function create($reply = NULL) {
		$this->session();
		$data['title'] = 'Messages - iTamCity';
		$model = new pm();
		$userModel = new user();
		$user = $userModel->findUser( $reply );
		$senderName = ucwords( strtolower( $user[0]['lastname'] )). ', ' . ucwords( strtolower( $user[0]['firstname'] ));
		$inbox = $model->countInbox($_SESSION['stud_no']);
		if( $reply !== NULL )	$sender = $reply;
		else	$sender = "";
		$data['content'] = 
			BufferedView::instance('messages/forms/create',array(
				"receiver" => $sender,
				"receiverName" => $senderName
			))->getBuffer();
		$data['globalnav'] = new BufferedView('org/globalnav');
		$data['usernav'] = BufferedView::instance('messages/nav', array('highlight'=>'create') );		
		new View ("template", $data);
	}

	public function send() {
		$this->session();
		$model = new pm();
		$date = gmdate ( 'YmdHis' );
		$model->insertPm($_SESSION['stud_no'], $_POST['to'], $date, $_POST['subject'] , $_POST['message'] ); 
		$message = 'Your message has been sent. Click <a href="/messages">here</a> to go back to the index.' ;
		new View('template', array( 
			'title' => 'Messages- iTamCity',
			'content' => $message,
			'globalnav' => new BufferedView('org/globalnav'),
			'usernav' => BufferedView::instance('messages/nav')
		) );
	}

	public function sent() {
		$this->session();
		//$data['title'] = new BufferedView('members/tsentMessage');
		$model = new pm();
		$messages = $model->getSent($_SESSION['stud_no']);
		$inbox = $model->countInbox($_SESSION['stud_no']);
		$data['title'] = 'Messages - iTamCity';
		$data['content'] = 
			BufferedView::instance('messages/sent',array(
				"inbox" => $inbox,
				"messages" => $messages
			) )->getBuffer();
		$data['heading'] = 'Sent Messages';
		$data['globalnav'] = new BufferedView('org/globalnav');
		$data['usernav'] = BufferedView::instance('messages/nav', array('highlight'=>'sent') );		
		new View ("template", $data);
	}
	
	public function showSentMessage ($messageId) {
	
		$this->session();
		$data['title'] = new BufferedView('members/tviewSentMessage');
		$model = new pm();
		$messagebyId = $model->getsentById($_SESSION['stud_no'],$messageId);
		$inbox = $model->countInbox($_SESSION['stud_no']);
		$data['content'] = new BufferedView('members/viewSentMessage',array(
			 "inbox" => $inbox,
			 'message' => $messagebyId[0]
		) );
      $data['globalnav'] = new BufferedView('org/globalnav');
		$data['usernav'] = BufferedView::instance('messages/nav');
		new View ("template", $data);
	}

	public function delete($param = NULL) {
		$this->session();
		if( $_POST['msgids'] === NULL ) {
			if( $_POST['referrer'] == 'inbox' ) $link = '/messages' ;
			if( $_POST['referrer'] == 'sent' ) $link = '/messages/sent';
			new View('template', 
				array( 
					'content' => 'You did not select a message to delete. Click <a href="' . $link . '">here</a> to go back.',
					'globalnav' => new BufferedView('org/globalnav'),
					'usernav' => BufferedView::instance('messages/nav')
				) 
			);
		} 
		else {
			// pag confirm na
			if( $param === 'confirm' ) {
				if( isset( $_POST['confirm-yes'] ) ) {
					$pm = new pm();
					$pm->deletePMs( $_POST['msgids'] );
					if( $_POST['referrer'] == 'inbox' ) $message = 'Your message has been deleted. Click <a href="/messages">here</a> to go back to inbox.' ;
					if( $_POST['referrer'] == 'sent' ) $message = 'Your message has been deleted. Click <a href="/messages/sent">here</a> to go back to sent messages.';
					new View('template', 
						array( 
							'content' => $message,
							'globalnav' => new BufferedView('org/globalnav'),
							'usernav' => BufferedView::instance('messages/nav')
						) 
					);
				}
				if( isset( $_POST['confirm-no'] ) ) {
					if( $_POST['referrer'] == 'inbox' ) header('Location: /messages');
					if( $_POST['referrer'] == 'sent' ) header('Location: /messages/sent');
					header('Location: /messages'); // url na babalikan pag pinindot ang no
				}
			}
			// pag wala pang confirm
			elseif( $param === NULL ) {
				if( $_POST['referrer'] == 'inbox' ) $hiddenInputs['referrer'] = 'inbox';
				if( $_POST['referrer'] == 'sent' ) $hiddenInputs['referrer'] = 'sent';
				$hiddenInputs['msgids'] = $_POST['msgids'];
				$confirmVars['hiddenInputs'] = $hiddenInputs;
				$confirmVars['action'] = '/messages/delete/confirm';
				$confirmForm = BufferedView::instance('public/forms/confirm', $confirmVars)->getBuffer();	
				new View(
					'template',
					array(
						'title' => 'Messages - iTamCity',
						'content' => 'Are you sure you want to delete these messages?' . $confirmForm,
						'usernav' => BufferedView::instance('messages/nav'),
						'globalnav' => new BufferedView('org/globalnav'),
						'usernav' => BufferedView::instance('messages/nav')
					)
				);
			}
		}
		
		echo $confirm;
		
	}

	public function action ()
	{
		if ($_POST['action'] === "Delete")
			$this->delete();
		if ($_POST['action'] === "Reply")
			header('Location: /messages/create/' . $_POST['sender']);

	}
		
	
}

